﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    public class Bullet
    {
        public Alien owner;
        public Vector2 pos;
        public Vector2 size = new Vector2(16, 16);
        public float radius;
        public Vector2 dir;
        public Color color0;  
        public Color color1;
        public float speed;
        public bool live = true;
        public float damage = 1;
        public float time = 0;
        public float blinkSpeed = 10;
        Sprite spr = new Sprite();

        public Bullet()
        {
            spr.color = Color.Orange;
            spr.origin = new Vector2(0.5f, 0.5f);
        }

        public Microsoft.Xna.Framework.Rectangle ComputeRect()
        {
            int x = (int)(pos.X - size.X * 0.5f);
            int y = (int)(pos.Y - size.Y * 0.5f);
            int w = (int)(size.X);
            int h = (int)(size.Y);
            return new Rectangle(x, y, w, h);
        }

        public bool Update(Timer t)
        {
            time += t.Delta;
            pos += dir * speed * t.Delta;

            if (Game1.instance.IsOutOfScreen(ComputeRect()))
                live = false;

            return live;
        }

        public void Draw(SpriteBatch sb)
        {
            float rotation = (float)Math.Atan2(dir.X, -dir.Y);
            spr.angle = 0; // rotation;
            spr.pos = pos;
            spr.size = size;
            spr.color = Color.Lerp( color0, color1, (float) Math.Sin( time * blinkSpeed ) * 0.5f + 0.5f) ;
            spr.Draw(sb);
        }

        public static Bullet CreateFatBullet()
        {
            Bullet b = new Bullet();
            b.speed = 250;
            b.color0 = Palette.alienBulletColor0;
            b.color1 = Palette.alienBulletColor1;
            b.size = new Vector2(24, 24);
            b.radius = 8;
            return b;
        }
    }
}
